package liurong.driver.file;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.MessageDigest;
import java.util.concurrent.TimeUnit;

public class FileTest {

	final static char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
	
	public static void main(String[] args) throws Exception {
		while (true) {
			System.out.println(calcHash());
			TimeUnit.SECONDS.sleep(3);
		}
	}

	private static String calcHash() throws Exception {
		Path path = Paths.get("D:\\a.txt");
		MessageDigest md = MessageDigest.getInstance("MD5");
		md.update(Files.readAllBytes(path));
		return bytes2string(md.digest());
	}

	public static String bytes2string(byte[] src) {
		char[] hexChars = new char[src.length * 2];
		for (int j = 0; j < src.length; j++) {
			int v = src[j] & 0xFF;
			hexChars[j * 2] = HEX_ARRAY[v >>> 4];
			hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
		}
		return new String(hexChars);
	}
}
